interface Options {
    timestamp?: number; // 时间戳，可选参数
    format?: '12' | '24'; // 时间格式，可选参数，默认为12小时制
    dateSeparator?: string; // 日期连接符，可选参数，默认为"-"
}
export function useFormattedTime(options?: Options) {
    const {
        timestamp = Date.now(),
        format = '12',
        dateSeparator = '-'
    } = options || {};

    const year = ref('');
    const month = ref('');
    const day = ref('');
    const weekDay = ref('');
    const formattedDate = ref('');
    const formattedTime = ref('');

    const updateTime = (time: number) => {
        const date = new Date(time);

        year.value = date.getFullYear().toString();
        month.value = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始，需要加1
        day.value = String(date.getDate()).padStart(2, '0');

        // 获取星期
        const weekDays = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
        weekDay.value = weekDays[date.getDay()];

        // 小时数处理
        let hours = date.getHours();
        const isPM = hours >= 12;

        if (format === '12') {
            hours = hours % 12 || 12; // 12小时制转换
        }

        const hour = String(hours).padStart(2, '0');
        const minute = String(date.getMinutes()).padStart(2, '0');
        const second = String(date.getSeconds()).padStart(2, '0');

        // 更新日期和时间
        formattedDate.value = `${year.value}${dateSeparator}${month.value}${dateSeparator}${day.value} ${format === '12' ? (isPM ? '下午' : '上午') : ''}`;
        formattedTime.value = `${hour}:${minute}:${second}`;
    };

    // 初始化时调用一次
    updateTime(timestamp);

    return {
        year,
        month,
        day,
        weekDay,
        formattedDate,
        formattedTime,
        updateTime
    };
}  